package com.sc.study.sleeproom.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sc.common.core.redis.RedisCache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.sleeproom.domain.ScSleepRoom;
import com.sc.study.sleeproom.service.IScSleepRoomService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;

/**
 * 寝室Controller
 * 
 * @author sc
 * @date 2022-03-14
 */
@RestController
@RequestMapping("/study/sleeproom")
public class ScSleepRoomController extends BaseController
{
    @Autowired
    private IScSleepRoomService scSleepRoomService;

    /**
     * 查询寝室列表
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproom:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScSleepRoom scSleepRoom)
    {
        startPage();
        List<ScSleepRoom> list = scSleepRoomService.selectScSleepRoomList(scSleepRoom);
        return getDataTable(list);
    }





    /**
     * 获取当前班级 管理的寝室
     * zwt
     * @param clazzId
     * @param sex
     */
    @GetMapping("/clazzForRoom")
    public AjaxResult getClazzForRooms(Integer clazzId, String sex){
        return AjaxResult.success(scSleepRoomService.getClazzForRooms(clazzId, sex));
    }

    /**
     * 查询所有寝室不分页
     * @param sleepRoomName   栋 + 层 + 名称 模糊查询
     * @param sex
     * @return
     */
    @GetMapping(value = "/listNotPage")
    public AjaxResult listNotPage(String sleepRoomName, String sex)
    {
        return AjaxResult.success(scSleepRoomService.getSleepRoomList(sleepRoomName, sex));
    }


    /**
     * 导出寝室列表
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproom:export')")
    @Log(title = "寝室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScSleepRoom scSleepRoom)
    {
        List<ScSleepRoom> list = scSleepRoomService.selectScSleepRoomList(scSleepRoom);
        ExcelUtil<ScSleepRoom> util = new ExcelUtil<ScSleepRoom>(ScSleepRoom.class);
        util.exportExcel(response, list, "寝室数据");
    }

    /**
     * 获取寝室详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(scSleepRoomService.selectScSleepRoomById(id));
    }

    /**
     * 新增寝室
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproom:add')")
    @Log(title = "寝室", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScSleepRoom scSleepRoom)
    {
        //设置id ： id为栋 + 楼层
        scSleepRoom.setId(Long.valueOf(scSleepRoom.getBuilding() + "" + scSleepRoom.getLayer()));

        return toAjax(scSleepRoomService.insertScSleepRoom(scSleepRoom));
    }

    /**
     * 修改寝室
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproom:edit')")
    @Log(title = "寝室", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScSleepRoom scSleepRoom)
    {
        return toAjax(scSleepRoomService.updateScSleepRoom(scSleepRoom));
    }

    /**
     * 删除寝室
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproom:remove')")
    @Log(title = "寝室", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scSleepRoomService.deleteScSleepRoomByIds(ids));
    }
}
